﻿
using Autofac.Core;
using Es.Abp.Application;
using Es.Abp.IApplication;
using Es.Abp.SqlSugarCore;
using Microsoft.OpenApi.Models;
using Volo.Abp.AspNetCore;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Swashbuckle;
using Microsoft.Extensions.DependencyInjection;
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
using Yi.Framework.AspNetCore;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace Es.Abp.Host
{
    [DependsOn(typeof(AbpAspNetCoreModule))]
    [DependsOn(typeof(AbpSwashbuckleModule))]
    [DependsOn(typeof(AbpAutofacModule))]

    [DependsOn(typeof(EsAbpSqlSugarCoreModule))]
    [DependsOn(typeof(EsAbpApplicationModule))]
    [DependsOn(typeof(YiFrameworkAspNetCoreModule))]


    public class EsAbpHostModule:AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            //base.ConfigureServices(context);
            var services = context.Services;
            services.AddControllers();
            services.AddEndpointsApiExplorer();
           
            //动态Api
            Configure<AbpAspNetCoreMvcOptions>(options =>
            {
                options.ConventionalControllers.Create(typeof(EsAbpApplicationModule).Assembly, option => option.RemoteServiceName = "Default");

            });
            //service.AddControllers();
            //Swagger

            // services.AddAbpSwaggerGen(
            //    options =>
            //    {
            //        options.SwaggerDoc("v1", new OpenApiInfo { Title = "default", Version = "v1" });
            //        options.DocInclusionPredicate((docName, description) => true);
            //        options.CustomSchemaIds(type => type.FullName);
            //    }
            //);
            //Swagger
            context.Services.AddYiSwaggerGen<EsAbpHostModule>(options =>
            {
                options.SwaggerDoc("default", new OpenApiInfo { Title = "Yi.Framework.Abp", Version = "v1", Description = "集大成者" });
            });


            //Swagger
            context.Services.AddSession(options =>
            {
                options.Cookie.Name = "MyAppSession";
                options.IdleTimeout = TimeSpan.FromMinutes(30); // 会话过期时间
                options.Cookie.HttpOnly = true; // 禁止通过JavaScript访问
            });
            services.AddHttpContextAccessor();

            context.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie()
                .AddJwtBearer();

        }
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            //base.OnApplicationInitialization(context);

            // Configure the HTTP request pipeline.
            var app =context.GetApplicationBuilder();
            var environment = context.GetEnvironment();
            if (environment.IsDevelopment())
            {
                //app.UseSwagger();
                //app.UseSwaggerUI();
            }
            app.UseSession(); // 启用Session中间件
            app.UseRouting();
            app.UseAuthorization();
            app.UseAuthentication();
            app.UseYiSwagger();

            //app.MapControllers();
            app.UseConfiguredEndpoints();
        }

    }
}
